//
//  LEReplyListController.m
//  LoveExpressBusiness
//
//  Created by 刘甲奇 on 2017/5/11.
//  Copyright © 2017年 DFH. All rights reserved.
//

#import "LEReplyListController.h"
#import "LEReplyHeader.h"
#import "LEReplyListCell.h"
#import "LEReplyModel.h"

#import "LEReplyDetialController.h"

static NSString *const LEReplyListCellId = @"LEReplyListCell";

@interface LEReplyListController ()

@end

@implementation LEReplyListController

- (void)setupUI
{
    [super setupUI];
    self.title = @"投诉列表";
    LEReplyHeader *replyHeader = (LEReplyHeader*)[UIView getNibView:@"LEReplyHeader"];
    [self.view addSubview:replyHeader];
    replyHeader.frame = CGRectMake(0, 0, LEScreenWidth, 60);
    
    [self.tableView registerNib:[UINib nibWithNibName:@"LEReplyListCell" bundle:nil] forCellReuseIdentifier:LEReplyListCellId];
    self.tableView.rowHeight = 60;
    [self resetTablewFrame:CGRectMake(0, 60, LEScreenWidth, LEScreenHeight -64 -60)];
    
    [self initHeader];
    [self initFooter];
}
- (void)viewWillAppear:(BOOL)animated
{
    [self.tabBarController.tabBar setHidden:YES];
}
- (void)loadData
{
    self.page = 1;
    [self requestRefreshData];
}
#pragma mark- 网络请求
- (void)requestRefreshData
{
    NSMutableDictionary *dict = [NSMutableDictionary dictionary];
    [dict setObject:@(self.page) forKey:@"curpage"];
    [dict setObject:@"10" forKey:@"page"];
    
    [SVProgressHUD showWithStatus:@"正在加载中"];
    __weak typeof (self) weakSelf = self;
    [API requestAppendKey:ServiceReplyListUrl params:dict success:^(NSDictionary *successCode) {
        [SVProgressHUD dismiss];
        LELog(@"%s---%d---%@",__func__,__LINE__,successCode);
        id datas = successCode[@"datas"];
        NSString *totalPage = successCode[@"page_total"];
        weakSelf.totalPage = totalPage;
        if ([datas isKindOfClass:[NSDictionary class]])
        {
            NSString *error = datas[@"error"];
            if (error)
            {
                [self addNoDataViewWithTipText:error];
            }
        }else if ([datas isKindOfClass:[NSArray class]])
        {
            [self hiddenNoDataView];
            NSArray *tempModel = [LEReplyModel mj_objectArrayWithKeyValuesArray:datas];
            
            if (weakSelf.page == 1) {
                [weakSelf.dataSource removeAllObjects];
                [weakSelf.dataSource addObjectsFromArray:tempModel];
                
            }else{
                [weakSelf.dataSource addObjectsFromArray:tempModel];
            }
            [weakSelf.tableView reloadData];
        }
        
    } falure:^(NSError *er) {
        LELog(@"%s---%d---%@",__func__,__LINE__,er);
        [SVProgressHUD dismiss];
    }];
}

#pragma mark- tableViewDelegate
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    return self.dataSource.count;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEReplyListCell *cell = [tableView dequeueReusableCellWithIdentifier:LEReplyListCellId forIndexPath:indexPath];
    cell.model = self.dataSource[indexPath.row];
    
    return cell;
}
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    LEReplyDetialController *detail = [[LEReplyDetialController alloc] init];
    detail.model = self.dataSource[indexPath.row];
    [self.navigationController pushViewController:detail animated:YES];
}

@end
